/*
 * Created by JFormDesigner on Mon Jul 01 14:48:36 CST 2024
 */

package scct.yzx.ui;

import scct.yzx.dao.ContactDao;
import scct.yzx.model.Contact;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 张
 */
public class PanelContactManger extends JPanel {
    DefaultTableModel Searchmodel = null;
    DefaultTableModel model;
    FormMain main;
    public PanelContactManger(FormMain main) {
        this.main = main;
        initComponents();
        initView();
    }
    private void initView() {
//        自己的初始化
//        获取表格对象的数据模型，通过对模型的操作，可以为表格添加和显示数据
        model = (DefaultTableModel) tabContact.getModel();
        model.addColumn("id");
        model.addColumn("名字");
        model.addColumn("用户名");
        model.addColumn("关系");
        getTable();
    }

    private void getTable(){
        model.setRowCount(0);
        ContactDao contactDao = new ContactDao();
        List<Contact> allContact = contactDao.getAllContact();
        for (Contact contact:allContact){
            model.addRow(new Object[]{contact.getId(),contact.getName(),contact.getUsername(),contact.getRelationship()});
        }
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgContactAdd dlgContactAdd = new DlgContactAdd(main);
        dlgContactAdd.setModal(true);
        dlgContactAdd.setVisible(true);
        getTable();

    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
        //        判断用户是否选中表中的一行
        if (tabContact.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择要修改的用户");
            return;
        }
        int index = tabContact.getSelectedRow();
//        System.out.println(index);
        int id = Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id);
        DlgContactUpdate dlgContactUpdate = new DlgContactUpdate(main,id);
        dlgContactUpdate.setModal(true);
        dlgContactUpdate.setVisible(true);
        getTable();
    }

    private void btnDel(ActionEvent e) {
        // TODO add your code here
        if (tabContact.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择要删除的用户");
            return;
        }
        int index = tabContact.getSelectedRow();
        System.out.println(index);
        int id = Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id);
        DlgContactDelete dlgContactDelete = new DlgContactDelete(main,id);
        dlgContactDelete.setModal(true);
        dlgContactDelete.setVisible(true);
        getTable();


    }

    private void btnSearch(ActionEvent e) {
        // TODO add your code here

        System.out.println(e.getSource());
        tabContact.setModel(model);
        Searchmodel = new DefaultTableModel();
        Searchmodel.addColumn("id");
        Searchmodel.addColumn("名字");
        Searchmodel.addColumn("用户名");
        Searchmodel.addColumn("关系");
        Searchmodel.setRowCount(0);
        String searchkey = txtSearch.getText();
        System.out.println(searchkey);
        String searchList = (String) comSearch.getSelectedItem();
        System.out.println(searchList);
        int row = tabContact.getRowCount();
        System.out.println("row:" + row);
        int column = tabContact.getColumnCount();
        System.out.println("column:" + column);
        for (int i = 0; i < row; i++) {
            if (tabContact.getRowCount() < row){
            }else {
                for (int j = 0; j < column; j++) {


                    if (Objects.equals(searchList, tabContact.getColumnName(j))) {
                        System.out.println("单元格的值：" + tabContact.getValueAt(i, j));
                        String cellValue = tabContact.getValueAt(i, j).toString().toLowerCase();
                        System.out.println("cellValue:" + cellValue);
                        if (cellValue.contains(searchkey)) {
                            Object[] rowDate = new Object[tabContact.getColumnCount()];
                            for (int k = 0; k < tabContact.getColumnCount(); k++) {
                                rowDate[k] = tabContact.getValueAt(i, k);
                                System.out.println(rowDate[k]);
                            }
                            Searchmodel.addRow(rowDate);
                            tabContact.setRowSelectionAllowed(true);
                            break;
                        }

                    }
                }
            }
        }
        tabContact.setModel(Searchmodel);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        btnDel = new JButton();
        txtSearch = new JTextField();
        btnSearch = new JButton();
        comSearch = new JComboBox<>();
        scrollPane1 = new JScrollPane();
        tabContact = new JTable();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing. border. EmptyBorder
        ( 0, 0, 0, 0) , "JF\u006frmDes\u0069gner \u0045valua\u0074ion", javax. swing. border. TitledBorder. CENTER, javax. swing. border
        . TitledBorder. BOTTOM, new Font ("D\u0069alog" , Font .BOLD ,12 ), Color. red) , getBorder( )) );  addPropertyChangeListener (new java. beans. PropertyChangeListener( ){ @Override public void
        propertyChange (java .beans .PropertyChangeEvent e) {if ("\u0062order" .equals (e .getPropertyName () )) throw new RuntimeException( )
        ; }} );
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);
            btnAdd.setBounds(new Rectangle(new Point(440, 10), btnAdd.getPreferredSize()));

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);
            btnUpdate.setBounds(new Rectangle(new Point(530, 10), btnUpdate.getPreferredSize()));

            //---- btnDel ----
            btnDel.setText("\u5220\u9664");
            btnDel.addActionListener(e -> btnDel(e));
            panel1.add(btnDel);
            btnDel.setBounds(new Rectangle(new Point(624, 10), btnDel.getPreferredSize()));
            panel1.add(txtSearch);
            txtSearch.setBounds(115, 10, 150, txtSearch.getPreferredSize().height);

            //---- btnSearch ----
            btnSearch.setText("\u641c\u7d22");
            btnSearch.addActionListener(e -> btnSearch(e));
            panel1.add(btnSearch);
            btnSearch.setBounds(new Rectangle(new Point(275, 10), btnSearch.getPreferredSize()));

            //---- comSearch ----
            comSearch.setModel(new DefaultComboBoxModel<>(new String[] {
                "id",
                "\u540d\u5b57",
                "\u7528\u6237\u540d",
                "\u5173\u7cfb"
            }));
            panel1.add(comSearch);
            comSearch.setBounds(15, 10, 95, comSearch.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabContact);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JButton btnDel;
    private JTextField txtSearch;
    private JButton btnSearch;
    private JComboBox<String> comSearch;
    private JScrollPane scrollPane1;
    private JTable tabContact;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
